﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Financeiro.Model;
using System.Data;

namespace Financeiro.Account
{
    public partial class GerenciarLogins : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            carregarGrid();
        }

        protected void btnRegistrar_Click(object sender, EventArgs e)
        {
            Response.Redirect("Register.aspx");
        }

        private void carregarGrid()
        {
            DataTable dt = LoginModel.buscarLogins();
            grdLogins.DataSource = dt;
            grdLogins.DataBind();
        }

        protected void grdLogins_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            if (grdLogins.Rows.Count <= 1)
            {
                // não permite apagar o ultimo login disponivel
                Response.Write("<script language='javascript'>alert('Erro! Mínimo de 1 login');</script>");
                
            }
            else
            {
                // pega o id a ser apagado
                int id = Convert.ToInt32(grdLogins.Rows[e.RowIndex].Cells[1].Text);

                // encaminha para a camada de modelo
                LoginModel.apagaLogin(id);

                // armazena no log
                if (Session["idAdmin"] != null)
                {
                    LogModel.deleteLog((int)Session["idAdmin"]);
                }

                // atualiza a pagina
                Response.Redirect("GerenciarLogins.aspx");
            }

            
        }


    }
}